package ru.aslanov.schedule.model;

import ru.aslanov.schedule.server.InputLangUtil;

import javax.jdo.annotations.*;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Created: Nov 30, 2009 6:51:38 PM
 *
 * @author Sergey Aslanov
 */
@XmlRootElement(name = "schedule")
@XmlType(propOrder = {"teachers", "locations", "dances", "levels", "calendars", "groups"})
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Schedule extends Entity {
    @Persistent
    private String name;
    @Persistent(dependent = "true")    
    private Document publishedDocument;

    @Persistent
    private Date loadedFromGCal;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name"))
    private List<Teacher> teachers;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name"))
    private List<Location> locations;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name"))
    private List<Dance> dances;

    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name"))
    @Element(dependent = "true")
    @Persistent(mappedBy = "schedule")
    private List<Level> levels;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    private List<Group> groups;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    private List<User> users;

    @Persistent(mappedBy = "schedule")
    @Element(dependent = "true")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name"))
    private List<GCalendar> calendars;

    @Persistent(mappedBy = "schedule")
    private List<InputLang> inputLangs;

    @Persistent
    private String defaultInputLanguage;

    @Persistent(dependent = "true")
    private GoogleCalendarSync googleCalendarSync;

    @Persistent
    @Embedded(members = {@Persistent(name="dbValue", columns=@Column(name="publishUrl"))})
    private I18nString publishUrl = new I18nString();

    @Persistent
    @Embedded(members = {@Persistent(name="dbValue", columns=@Column(name="city"))})
    private I18nString city = new I18nString();

    @Persistent
    private String createdBy;

    public Schedule(String name) {
        this.name = name;
    }

    public Schedule(List<Group> groups) {
        this.groups = groups;
    }

    public Schedule() {
        groups = new ArrayList<Group>();
    }

    @XmlElementWrapper(name = "groups")
    @XmlElement(name = "group")
    public List<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }

    @XmlElementWrapper(name = "teachers")
    @XmlElement(name = "teacher")
    public List<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    @XmlElementWrapper(name = "locations")
    @XmlElement(name = "location")
    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    @XmlElementWrapper(name = "dances")
    @XmlElement(name = "dance")
    public List<Dance> getDances() {
        return dances;
    }

    public void setDances(List<Dance> dances) {
        this.dances = dances;
    }

    @XmlElementWrapper(name = "levels")
    @XmlElement(name = "level")    
    public List<Level> getLevels() {
        return levels;
    }

    public void setLevels(List<Level> levels) {
        this.levels = levels;
    }

    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Document getPublishedDocument() {
        return publishedDocument;
    }

    public void setPublishedDocument(Document publishedDocument) {
        this.publishedDocument = publishedDocument;
    }

    @XmlTransient
    public List<User> getUsers() {
        return users;
    }

    @XmlElementWrapper(name = "g-calendars")
    @XmlElement(name = "g-calendar")
    public List<GCalendar> getCalendars() {
        return calendars;
    }

    public void setCalendars(List<GCalendar> calendars) {
        this.calendars = calendars;
    }

    @XmlTransient
    public Date getLoadedFromGCal() {
        return loadedFromGCal;
    }

    public void setLoadedFromGCal(Date loadedFromGCal) {
        this.loadedFromGCal = loadedFromGCal;
    }

    @XmlTransient
    public GoogleCalendarSync getGoogleCalendarSync() {
        return googleCalendarSync;
    }

    public void setGoogleCalendarSync(GoogleCalendarSync googleCalendarSync) {
        this.googleCalendarSync = googleCalendarSync;
    }

    @XmlTransient
    public String getPublishUrl() {
        return publishUrl.getValueWithThreadLang();
    }

    public void setPublishUrl(String publishUrl) {
        this.publishUrl.setValueWithThreadLang(publishUrl);
    }

    @XmlAttribute
    public String getCity() {
        return city.getValueWithThreadLang();
    }

    @XmlTransient
    public I18nString getCityI18n() {
        return city; 
    }

    @XmlTransient
    public I18nString getPublishUrlI18n() {
        return publishUrl;
    }

    public void setCity(String city) {
        this.city.setValueWithThreadLang(city);
    }

    @XmlTransient
    public List<InputLang> getInputLangs() {
        return inputLangs;
    }

    @XmlTransient
    public String getDefaultInputLanguage() {
        return defaultInputLanguage;
    }

    public void setDefaultInputLanguage(String defaultInputLanguage) {
        this.defaultInputLanguage = defaultInputLanguage;
    }

    @XmlAttribute(name = "lang")
    public String getCurrentLang() {
        return InputLangUtil.getThreadLocalInputLang();    
    }

    @XmlTransient
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
}
